package hongsong.eqixueweb.examcenter;

import static hongsong.eqixueweb.common.PagesXpath.xpath_myexam;
import static hongsong.eqixueweb.common.PagesXpath.xpath_questionbank;
import static hongsong.eqixueweb.common.PagesXpath.xpath_markingroom;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.testng.annotations.Test;

import hongsong.eqixueweb.common.BasicAction;
import hongsong.eqixueweb.common.DateFromat;

public class MarkingRoom extends BasicAction {
	/*
	 * 搜索阅卷试卷
	 */
	public void srchmark(String examname) {

		// *[@id="keyword"]
		BasicAction.sleep(3);
		webwait.until(ExpectedConditions.presenceOfElementLocated(By.id("keyword"))).click();
		driver.findElement(By.id("keyword")).sendKeys(examname);
		driver.findElement(By.id("keyword")).sendKeys(Keys.ENTER);

		// 点击阅卷试卷//*[@id="examReadContent"]/div
		webwait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"examReadContent\"]/div")));
		wAction = driver.findElement(By.xpath("//*[@id=\"examReadContent\"]/div"));
		((JavascriptExecutor) driver).executeScript("arguments[0].click();", wAction);
		BasicAction.sleep(2);

		// 点击阅卷
		webwait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"content\"]/tr")));
		wAction = driver.findElement(By.xpath("//*[@id=\"content\"]/tr"));
		((JavascriptExecutor) driver).executeScript("arguments[0].click();", wAction);
		BasicAction.sleep(2);

		Set<String> winHandels = driver.getWindowHandles(); // 得到当前窗口的set集合
		List<String> it = new ArrayList<String>(winHandels); // 将set集合存入list对象
		driver.switchTo().window(it.get(1)); // 切换到弹出的新窗口
		BasicAction.sleep(2);

		// 输入填空阅卷分数

		webwait.until(ExpectedConditions
				.presenceOfElementLocated(By.xpath("//*[@id=\"blanks\"]/div[2]/div/section/div[3]/p[2]/input")))
				.sendKeys("1");

		BasicAction.sleep(2);

		// 点击提交阅卷//*[@id="isSave"]
		webwait.until(ExpectedConditions.presenceOfElementLocated(By.id("isSave")));
		wAction = driver.findElement(By.id("isSave"));
		((JavascriptExecutor) driver).executeScript("arguments[0].click();", wAction);
		BasicAction.sleep(2);

		// 点击确认提交阅卷/html/body/div[5]/div[7]/button[2]
		webwait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[5]/div[7]/button[2]")));
		wAction = driver.findElement(By.xpath("/html/body/div[5]/div[7]/button[2]"));
		((JavascriptExecutor) driver).executeScript("arguments[0].click();", wAction);
		BasicAction.sleep(2);
		driver.close();
		driver.switchTo().window(it.get(0)); // 切换到弹出的新窗口
		BasicAction.sleep(2);
	}

	@Test
	public void f() {
		DateFromat df = new DateFromat();
		String addquname = "题库" + df.getdate();
		String examname = "考试" + df.getdate();
		ExamMagTest et = new ExamMagTest();
		et.releaseexam(examname, addquname);
		// 点击考试管理菜单

		BasicAction.sleep(2);
		driver.findElement(By.id(xpath_myexam)).click();
		BasicAction.sleep(2);
		MyexamTest mt = new MyexamTest();
		// 参加考试
		mt.joinexam(examname);

		//点击阅卷室
		webwait.until(ExpectedConditions.presenceOfElementLocated(
				By.id(xpath_markingroom)));
		wAction = driver
				.findElement(By.id(xpath_markingroom));
		((JavascriptExecutor) driver).executeScript("arguments[0].click();", wAction);
	
		this.srchmark(examname);
		// 点击题库管理菜单

		BasicAction.sleep(2);
		driver.findElement(By.xpath(xpath_questionbank)).click();
		BasicAction.sleep(2);
		QuestionMagTest qmt = new QuestionMagTest();
		qmt.delqumall(addquname);
	}
}
